"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const turf = __importStar(require("@turf/turf"));
const ProgressBar_1 = __importDefault(require("../ProgressBar/ProgressBar"));
const arrChunk_1 = __importDefault(require("../arrChunk"));
const pg = new ProgressBar_1.default('聚合多边形', 40);
let featuresLength = 0;
let completedIndex = 0;
function union(sourceGeoJson) {
    let resGeoJson;
    turf.featureEach(sourceGeoJson, function (currentFeature, featureIndex) {
        if (featureIndex === 0) {
            resGeoJson = currentFeature;
        }
        else {
            resGeoJson = turf.union(resGeoJson, currentFeature);
        }
        completedIndex++;
        pg.render({
            completed: completedIndex,
            total: featuresLength,
            description: '聚合多边形'
        });
    });
    return resGeoJson;
}
function unionGeoJson(geoJson) {
    const sourceGeoJson = turf.flatten(geoJson);
    featuresLength = sourceGeoJson.features.length;
    completedIndex = 0;
    const group = [];
    if (featuresLength > 200) {
        const chunkSize = Math.ceil(featuresLength / 10);
        const chunk = (0, arrChunk_1.default)(sourceGeoJson.features, chunkSize);
        for (let i = 0; i < chunk.length; i++) {
            group.push(chunk[i]);
        }
    }
    else {
        group.push(sourceGeoJson.features);
    }
    const groupGeoJson = [];
    for (let i = 0; i < group.length; i++) {
        const collection = turf.featureCollection(group[i]);
        groupGeoJson.push(union(collection));
    }
    let sumGeoJson = union(turf.featureCollection(groupGeoJson));
    return turf.flatten(sumGeoJson);
}
exports.default = unionGeoJson;
